package com.basker.pisces.core.meta.bean;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.basker.pisces.core.meta.IDataObjectMeta;
import com.basker.pisces.core.meta.utils.DataObjectMetaUtils;

public class ReferenceBeanMeta extends PartBeanMeta {

    public ReferenceBeanMeta(Class<?> beanClass, String[] loadFields) {
        super(beanClass, loadFields);
        this.setReadOnly(true);
    }

    @Override
    public boolean isReadOnly() {
        return true;
    }

    @Override
    protected IDataObjectMeta createCollectionMeta(String fieldName, Class<?> itemClass) {
        List<String> itemMetaLoadFields = DataObjectMetaUtils.filterByPrefix(fieldName, Arrays.asList(loadFields),
                true);

        return BeanMetaManager.getReferenceBeanMeta(itemClass,
                itemMetaLoadFields.toArray(new String[itemMetaLoadFields.size()]));
    }

    @Override
    protected Set<String> getRefFieldSet(IDataObjectMeta refMeta, String[] otherRefFields) {
        Set<String> refFields = new HashSet<>();
        // 当前契约是引用成员的契约，其引用成员的契约只包含id、代码、名称
        refFields.addAll(DataObjectMetaUtils.getIdCodeNameFields(refMeta));

        return refFields;
    }
}
